

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.*;
import javax.servlet.http.*;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;

@SuppressWarnings("serial")
public class ShopServlet extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		Document shopXml;
		PrintWriter out = response.getWriter();
		Namespace nameSpace = Namespace.getNamespace("http://www.cs.au.dk/dWebTek/2011");

		out.println("<select style=\"width: 120pt\" onchange=\"gotoShop(this)\">");
		out.println("<optgroup label=\"Visit another shop\">" +
				"<option selected=\"selected\" value=\"\">MovieShop</option>");
		try {
			shopXml = new SAXBuilder().build("http://services.brics.dk/java/cloud/listShops");
			List<?> shops = shopXml.getRootElement().getChildren("shop", nameSpace);
			for(Object shop : shops){
				Element ele = (Element) shop;
				out.print("<option value=\"" + ele.getChildText("shopURL", nameSpace) + "\">");
				out.print(ele.getChildText("shopName", nameSpace));
				out.println("</option>");
			}
		} catch (JDOMException e) {
			e.printStackTrace();
		}
		out.println("</optgroup></select>");
	}
}
